//
//  YTDriverAuthBasicViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/12.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTDriverAuthBasicViewController.h"
#import "YTDriverAuthBasicViewModel.h"
#import "YTCitySeletedViewController.h"
#import "YTPickerView.h"
#import "YTProfileModel.h"

@interface YTDriverAuthBasicViewController ()

@property (weak, nonatomic) IBOutlet UITextField *realNameText;
@property (weak, nonatomic) IBOutlet UIButton *maleBtn;
@property (weak, nonatomic) IBOutlet UIButton *femaleBtn;
@property (weak, nonatomic) IBOutlet UILabel *birthDayText;
@property (weak, nonatomic) IBOutlet UILabel *serviceCitysText;
@property (weak, nonatomic) IBOutlet UILabel *liveCityText;
@property (weak, nonatomic) IBOutlet UILabel *idTypeText;
@property (weak, nonatomic) IBOutlet UITextField *idNumberText;
@property (weak, nonatomic) IBOutlet UIButton *faceImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *backImageBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextStepBtn;

@property (strong, nonatomic) UIImage *faceImage;
@property (strong, nonatomic) UIImage *backImage;

@property (copy, nonatomic) NSString *male;
@property (strong, nonatomic) YTDriverAuthBasicViewModel *viewModel;

@end

@implementation YTDriverAuthBasicViewController

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initialize {
    _viewModel = [[YTDriverAuthBasicViewModel alloc] init];
    _male = @"0";
    
    // 选取照片
    @weakify(self);
    [[_faceImageBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *faceButton) {
        @strongify(self);
        [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
            [faceButton setBackgroundImage:image forState:UIControlStateNormal];
            self.faceImage = image;
        }];
    }];
    
    [[_backImageBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *backButton) {
        @strongify(self);
        [self yt_selecteImageCompletion:^(UIImage * _Nullable image) {
            [backButton setBackgroundImage:image forState:UIControlStateNormal];
            self.backImage = image;
        }];
    }];
    
    // 性别选择
    [[_maleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *button) {
        button.selected = !button.selected;
        self.femaleBtn.selected = NO;
        self.male = @"0";
    }];
    [[_femaleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *button) {
        button.selected = !button.selected;
        self.maleBtn.selected = NO;
        self.male = @"1";
    }];
    
    [_viewModel.nextCommand.enabled subscribeNext:^(NSNumber *enable) {
        @strongify(self);
        self.nextStepBtn.backgroundColor = (enable.boolValue ? kMainBlueColor : kGrayColor);
    }];
}

- (void)bindViewModel {
    RAC(_viewModel, realName) = _realNameText.rac_textSignal;
    RAC(_viewModel, male) = RACObserve(self, male);
    RAC(_viewModel, birthDay) = RACObserve(_birthDayText, text);
    RAC(_viewModel, idType) = RACObserve(_idTypeText, text);
    RAC(_viewModel, idNum) = _idNumberText.rac_textSignal;
    RAC(_viewModel, faceImage) = RACObserve(self, faceImage);
    RAC(_viewModel, backImage) = RACObserve(self, backImage);
    
    RACChannelTerminal *live = RACChannelTo(_liveCityText, text);
    [_viewModel.liveCityTerminal subscribe:live];
    [live subscribe:_viewModel.liveCityTerminal];
    
    RACChannelTerminal *service = RACChannelTo(_serviceCitysText, text);
    [_viewModel.serviceCityTerminal subscribe:service];
    [service subscribe:_viewModel.serviceCityTerminal];
    
    _nextStepBtn.rac_command = _viewModel.nextCommand;
    @weakify(self);
    [[_viewModel.nextCommand.executionSignals switchToLatest] subscribeNext:^(NSDictionary *response) {
        @strongify(self);
        [self performSegueWithIdentifier:@"YTUploadDriverLicense" sender:nil];
    }];
    
    [_viewModel.nextCommand.executing subscribeNext:[self yt_showHudWithTips:@"拼命上传中..."]];
}

- (void)dealloc {}

#pragma mark - UITableViewDelegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    @weakify(self);
    switch (indexPath.row) {
        case 2: {
            YTPickerView *picker = [YTPickerView pickerWithType:YTPickerTypeMonthDay];
            picker.completion = ^(NSString *selectedString) {
                @strongify(self);
                self.birthDayText.textColor = kBlackColor;
                self.birthDayText.text = selectedString;
            };
            picker.tips = @"选择您的生日";
            picker.birthday = YES;
            [picker show];
        }
            break;
        case 5: {
            NSArray *array = @[@"身份证", @"护照", @"绿卡", @"学生证", @"当地身份证"];
            YTPickerView *picker = [YTPickerView pickerWithDataSource:array];
            picker.completion = ^(NSString *selectedString) {
                @strongify(self);
                self.idTypeText.textColor = kBlackColor;
                self.idTypeText.text = selectedString;
            };
            [picker show];
        }
            break;
        default:
            break;
    }
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    @weakify(self);
    if ([segue.identifier isEqualToString:@"YTSelectLiveCity"]) {
        YTCitySeletedViewController *city = segue.destinationViewController;
        city.isSeleteLive = YES;
        city.isInland = ([YTUtilityManager manager].userModel.isChina == 1);
        city.seletedCallBack = [RACSubject subject];
        [city.seletedCallBack subscribeNext:^(RACTuple *tuple) {
            @strongify(self);
            self.liveCityText.textColor = kBlackColor;
            [self.viewModel.liveCommand execute:tuple];
        }];
    }
    
    if ([segue.identifier isEqualToString:@"YTSelectServiceCity"]) {
        YTCitySeletedViewController *city = segue.destinationViewController;
        city.isInland = ([YTUtilityManager manager].userModel.isChina == 1);
        city.seletedCallBack = [RACSubject subject];
        [city.seletedCallBack subscribeNext:^(RACTuple *tuple) {
            @strongify(self);
            self.serviceCitysText.textColor = kBlackColor;
            [self.viewModel.serviceCommand execute:tuple];
        }];
    }
}
@end
